$(document).ready(function () {
    $("#search").focus(function () {
        $("#search").attr("value", "");

        if ($("#search").value != "") {
            document.getElementById("div_search").style.visibility = "visible";
        }
    });
    document.getElementById("search").addEventListener("input", search);
    $("#search").blur(function () {
        $("#search").attr("value", "请输入您想购买的商品名...");
        document.getElementById("div_search").style.visibility = "hidden";
    });


    function search() {
        $.ajax({
            "url": "foraging.do",
            "type": "post",
            "dataType": "json",
            "data": {
                "method": "search",
                "searchValue": $("#search").val(),
            },
            "success": function (result) {
                $("#list-tab a").remove();
                $.each(result, function (index, data) {
                    $("#list-tab").append(addRows(data))
                });
            }

        });
    }


    // style='background-color:rgba(0,0,0,0.2)'


});

function addRows(category) {
    // return $("<a class='list-group-item list-group-item-action'  href='category.do?method=categorySearch' id='href' role='tab' name='cateoryname' value='"+$("#href").innerHTML+"'"+">"+category.categoryname+"</a>"
    return $("<a class='list-group-item list-group-item-action' role='tab' href='javascript:turnToServlet(\"" + category.categoryname + "\");'>" + category.categoryname + "</a>"
    )


}

function turnToServlet(categoryname) {
    $.ajax({
        "url": "goodsServlet.do",
        "type": "post",
        "dataType": "json",
        "data": {
            "method": "categorySearch",
            "categoryname": categoryname,
        },
        "success": function (result) {
            alert(result);
            sessionStorage.obj = JSON.stringify(result);
            location.href = "categorySearch.jsp";

        }
    });
    // window.load("http://localhost:8080/foraging/cate");
}


